/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   MaintenanceFundsServiceImpl.java for com.pengtu.gsj.service.impl.check.zhgl.fundGuarantee    </p>
 * 
 * <p>最后修改 : $: 2017年8月16日-下午3:59:40 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.zhgl.fundGuarantee;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.zhgl.FundGuaranteeDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.zhgl.FundGuarantee;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.zhgl.fundGuarantee.MaintenanceFundsService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.StringUtils;

/**
 * 
 * MaintenanceFundsServiceImpl
 * 
 * 2017年8月16日 下午3:59:40
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class MaintenanceFundsServiceImpl implements MaintenanceFundsService{
	
	@Autowired
	private FundGuaranteeDao fundGuaranteeDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	@Override
	public List<FundGuarantee> getFundGuaranteeList(QueryFilter queryFilter){
		return fundGuaranteeDao.find(queryFilter, "testYear", true);
	}
	
	@Override
	public Map<String, Object> getFundGuaranteeMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<FundGuarantee> fundGuaranteeList = new ArrayList<FundGuarantee>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		
		//现场检查路线的名称
		QueryFilter queryFilter = new QueryFilter();
		//年度养护资金详情查询
		queryFilter.addFilter("testYear", startTime, MatchType.GE);
		queryFilter.addFilter("testYear", String.valueOf(Integer.valueOf(endTime)-1), MatchType.LE);
		
		double total =0; 		//检查得分
		double percentage =0; 	//系统完善度
		double tableFull  = yearFull;	//满分
		double sumMaintenanceMoney = 0;	//国省检时间段内养护资金支出总和
		double sumFiscalMoney = 0;		//国省检时间段内财政转移支付资金总和
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		fundGuaranteeList = this.getFundGuaranteeList(queryFilter);
		
		boolean flag = true;
		for (FundGuarantee fundGuarantee : fundGuaranteeList) {
			if (fundGuarantee.getIsEmbezzle().equals("0")) {
				if (!StringUtils.isEmpty(String.valueOf(fundGuarantee.getMaintenanceMoney())) && !StringUtils.isEmpty(String.valueOf(fundGuarantee.getFiscalMoney()))) {
					sumMaintenanceMoney = MathsUtils.sumDouble(sumMaintenanceMoney, fundGuarantee.getMaintenanceMoney());
					sumFiscalMoney = MathsUtils.sumDouble(sumFiscalMoney, fundGuarantee.getFiscalMoney());
				}
			} else {
				flag = false;
				break;
			}
		}
		
		if (flag) {
			total = MathsUtils.divDouble(sumMaintenanceMoney, sumFiscalMoney, 2);
			total = MathsUtils.subDouble(0.8, total)*100;
			total = MathsUtils.subDouble(yearFull, total);
		}
		
		if (total < 0) {
			total = 0;
		} else if (total > yearFull) {
			total = yearFull;
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,1));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		map.put("percent", percentage);
		map.put("years", years);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("overView", fundGuaranteeList);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
